﻿using Rhino.PlugIns;
using System;
using System.IO;
using System.Reflection;

namespace AiMoCore
{
    ///<summary>
    /// <para>Every RhinoCommon .rhp assembly must have one and only one PlugIn-derived
    /// class. DO NOT create instances of this class yourself. It is the
    /// responsibility of Rhino to create an CurrentScriptInstance of this class.</para>
    /// <para>To complete plug-in information, please also see all PlugInDescription
    /// attributes in AssemblyInfo.cs (you might need to click "Project" ->
    /// "Show All Files" to see it in the "Solution Explorer" window).</para>
    ///</summary>
    public class AiMoPlugin : PlugIn
    {
        public AiMoPlugin()
        {
            Instance = this;
            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
        }

        private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            string[] names = args.Name.Split(',');
            if (names.Length > 0 && names[0].Equals("AiMoBridge", StringComparison.OrdinalIgnoreCase))
            {
                FileInfo rhpFile = new FileInfo(typeof(AiMoPlugin).Assembly.Location);
                string path = Path.Combine(rhpFile.Directory.FullName, "lib", "AiMoBridge.dll");
                if (File.Exists(path))
                {
                    return Assembly.LoadFrom(path);
                }
            }
            return null;
        }
        public override PlugInLoadTime LoadTime => PlugInLoadTime.AtStartup;
        ///<summary>Gets the only CurrentScriptInstance of the AiMoPlugin plug-in.</summary>
        public static AiMoPlugin Instance { get; private set; }

        // You can override methods here to change the plug-in behavior on
        // loading and shut down, add options pages to the Rhino _Option command
        // and maintain plug-in wide options in a document.
    }
}

// Visual Installer 说明
// https://learn.microsoft.com/zh-cn/windows/win32/msi/s-gly